module app.controller.api.ArticleController;

// import std.stdio;

import std.json;
import hunt.framework;
import app.repository.ArticleRepository;
import app.response.ResultMsg;
import app.utils.RespCodeEnun;
import app.model.Article;
import app.utils.Const;

class ArticleController : Controller
{
    mixin MakeController;

    /**
    * 分页获取文章列表
    */
    @Action Response list()
    {
        import std.random;
        import std.string;
        import std.datetime.systime;

        // 获取查询参数 page
        int page = cast(int) request.get("page", 1);
        int perPage = cast(int) request.get("perPage", 7);
        // int page = cast(int)(this.request().get("page", 1));
        // string page = request.input("page", "1");
        // writefln("page: %s", page);

        // 当前页码
        int currPage = page;

        ResultMsg resultMsg = new ResultMsg();
        ArticleRepository articleRepository = new ArticleRepository();

        try
        {
            // 分页查询
            Page!Article artdata = articleRepository.findArticlesByPagination(page, perPage);

            // auto articles = articleRepository.findAll();
            // 查询到的文章
            auto articles = artdata.getContent();
            // 是否有下一页
            bool hasNextPage = artdata.hasNextPage();

            JSONValue[] items;
            foreach (article; articles)
            {
                JSONValue item;
                item["id"] = article.id;
                item["title"] = article.title;
                item["desc"] = article.desc;
                item["view_count"] = article.viewCount;
                item["comment_count"] = article.commentCount;
                // writeln(tr(SysTime(unixTimeToStdTime(article.createdAt))
                //         .toISOExtString(), "T", " "));
                item["created_at"] = tr(SysTime(unixTimeToStdTime(article.createdAt))
                        .toISOExtString(), "T", " ");

                item["img"] = Const.imgs.choice();
                items ~= item;
            }

            resultMsg.code = RespCodeEnun.SUCCESS;
            resultMsg.msg = "success";
            resultMsg.data["data"] = items;
            resultMsg.data["currPage"] = currPage;
            resultMsg.data["hasNextPage"] = hasNextPage;
        }
        catch (Exception e)
        {
            this.response.setStatus(500);
            resultMsg.code = RespCodeEnun.ERROR;
            resultMsg.msg = "系统异常";
        }

        return new JsonResponse(resultMsg);
    }

    /**
    * 文章详情
    * @id   int   文章id
    */
    @Action Response detail(long id)
    {
        import std.random;
        import std.string;
        import std.datetime.systime;

        ResultMsg resultMsg = new ResultMsg();
        ArticleRepository articleRepository = new ArticleRepository();

        Article article = articleRepository.findById(id);
        if (article is null)
        {
            this.response.setStatus(404);
            resultMsg.code = RespCodeEnun.Not_Found;
            resultMsg.msg = "未找到";
            resultMsg.data["data"] = null;
        }
        else
        {
            JSONValue item;
            item["id"] = article.id;
            item["cid"] = article.cid;
            item["title"] = article.title;
            item["desc"] = article.desc;
            item["content"] = article.content;
            item["view_count"] = article.viewCount;
            item["comment_count"] = article.commentCount;
            // 时间戳转正常日期时间
            item["created_at"] = tr(SysTime(unixTimeToStdTime(article.createdAt))
                    .toISOExtString(), "T", " ");
            item["updated_at"] = tr(SysTime(unixTimeToStdTime(article.updatedAt))
                    .toISOExtString(), "T", " ");

            item["img"] = Const.imgs.choice();

            resultMsg.code = RespCodeEnun.SUCCESS;
            resultMsg.msg = "success";
            resultMsg.data["data"] = item;
        }
        return new JsonResponse(resultMsg);
    }
}
